<template>
    <d2-container :class="{'page-compact':crud.pageOptions.compact}">
        <d2-crud-x
                ref="d2Crud"
                v-bind="_crudProps"
                v-on="_crudListeners"
                @planTest="planTest"
                @showPlanResult="showPlanResult">

            <div slot="header">
                <crud-search ref="search" :options="crud.searchOptions" @submit="handleSearch"  />
                <el-button size="small" type="primary" @click="addRow"><i class="el-icon-plus"/> 新增</el-button>
                <el-button size="small" type="danger" @click="batchDelete"><i class="el-icon-delete"></i> 批量删除</el-button>
                <crud-toolbar v-bind="_crudToolbarProps" v-on="_crudToolbarListeners"/>
            </div>
            <span slot="PaginationPrefixSlot" class="prefix" >
                <el-button class="square" size="mini" title="批量删除"   @click="batchDelete" icon="el-icon-delete" :disabled="!multipleSelection || multipleSelection.length===0"  />
            </span>
        </d2-crud-x>
        <el-drawer :visible.sync="drawer" :size="1500">
          <div slot="title">
            <span>测试结果</span>
              <el-tag size="small" style="margin-left: 10px">当前计划:{{
                apiRow.plan_name
              }}</el-tag>
              <!-- 添加统计信息标签 -->
              <el-tag v-if="statistics" size="small" type="success" style="margin-left: 10px">总数:{{statistics.total}}</el-tag>
              <el-tag v-if="statistics" size="small" type="success" style="margin-left: 10px">成功:{{statistics.success}}</el-tag>
              <el-tag v-if="statistics" size="small" type="danger" style="margin-left: 10px">失败:{{statistics.fail}}</el-tag>
              <el-tag v-if="statistics" size="small" type="warning" style="margin-left: 10px">跳过:{{statistics.skip}}</el-tag>
          </div>
          <plan-result
            ref="planResult"
            style="margin-top: 80px; margin-left: 10px"
            :planRow="apiRow"
            @statistics-updated="statistics = $event"
          >
          </plan-result>
        </el-drawer>
    </d2-container>
</template>

<script>
import { crudOptions } from './crud'
import { d2CrudPlus } from 'd2-crud-plus'
import * as api from './api'
import PlanResult from '@/views/test_plan/plan_result/index'
export default {
  name: 'test_plan',
  components: { PlanResult },
  mixins: [d2CrudPlus.crud],
  data () {
    return {
      drawer: false,
      apiRow: {},
      statistics: null
    }
  },
  watch: {
    apiRow () {
      this.doRefresh({ from: 'load' })
    }
  },
  methods: {
    getCrudOptions () {
      return crudOptions(this)
    },
    pageRequest (query) {
      return api.GetList(query)
    },
    addRequest (row) {
      return api.AddObj(row)
    },
    updateRequest (row) {
      return api.UpdateObj(row)
    },
    delRequest (row) {
      return api.DelObj(row.id)
    },
    batchDelRequest (ids) {
      return api.BatchDel(ids)
    },
    infoRequest (row) {
      return api.GetObj(row.id)
    },
    planTest ({ row }) {
      this.crud.loading = true
      api.planTest(row.id).then((res) => {
        this.$message.success('调试成功')
      }).catch(error => {
        this.crud.loading = false
        this.$message.error('调试失败：' + error)
      }).finally(() => {
        this.crud.loading = false
        this.doAfterRefresh()
      })
    },
    showPlanResult (scope) {
      this.drawer = true
      this.apiRow = scope.row
      this.statistics = null

      this.$nextTick(() => {
        // 获取子组件中的搜索表单引用并重置
        if (this.$refs.planResult && this.$refs.planResult.$refs.search) {
          this.$refs.planResult.$refs.search.handleFormReset()
        }
      })
    }
  },
  mounted () {
    this.$on('statistics-updated', (statistics) => {
      this.statistics = statistics
    })
  }
}
</script>
